package net.entelijan.cobean.examples.showcase.bind.table;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import net.entelijan.cobean.bind.impl.BoundPropBinding;
import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.core.ISingleSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.table.DefaultSingleSelectionTableInitializer;
import net.entelijan.cobean.text.DefaultTextInitializer;
import net.entelijan.cobean.text.converter.IntTextConverter;
import net.entelijan.scf.IShowcase;

public class MasterDetailTableShowcase implements
		IShowcase<MasterDetailTablePanel> {

	private ILitService service = null;

	private SingleSelectionModel model = new SingleSelectionModel();

	public MasterDetailTableShowcase() {
		super();
	}

	@Override
	public void init(final MasterDetailTablePanel view) {

		configure(view);

		// Fill the model with some books. As it is bound these books appear in
		// the view.
		model.setBooks(service.loadAllBooks());
		// Set one book as selected book. The book appears (automatically) selected in the
		// JTable because of the binding.
		model.setSelectedBook(model.getBooks().get(4));

		view.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(view, model.getSelectedBook());
			}

		});

		view.getSelectFirstBookButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				LitBook firstBook = model.getBooks().get(0);
				model.setSelectedBook(firstBook);
			}

		});

	}

	/**
	 * Creates the cobeans and binds them to the model object.
	 */
	private void configure(final MasterDetailTablePanel view) {
		{
			// Create the component beans
			ISingleSelectionListCobean<LitBook> tableCobean;
			{
				DefaultSingleSelectionTableInitializer<LitBook> initializer = new DefaultSingleSelectionTableInitializer<LitBook>();
				initializer.setCoTable(view.getTable());
				initializer.setCoFilterInput(view.getFilterText());
				initializer.setColumnDescs(ColumnDesc
						.createFilterableSortableColumnDescs("title,Title",
								"year,Publishing Year", "author.name, Author"));
				tableCobean = initializer.initialize();
			}
			ICobean<String> titleCobean;
			{
				DefaultTextInitializer<String> initializer = new DefaultTextInitializer<String>();
				initializer.setCoTextComponent(view.getTitleTextField());
				initializer.setCoFailureComponent(view.getFailureTextField());
				titleCobean = initializer.initialize();
			}
			ICobean<Integer> yearCobean;
			{
				DefaultTextInitializer<Integer> initializer = new DefaultTextInitializer<Integer>();
				initializer.setCoTextComponent(view.getYearTextField());
				initializer.setConverter(new IntTextConverter());
				initializer.setCoFailureComponent(view.getFailureTextField());
				initializer
						.setFailureBackgroundColor(DefaultTextInitializer.COLOR_YELLOW);
				yearCobean = initializer.initialize();
			}
			ICobean<String> authorCobean;
			{
				DefaultTextInitializer<String> initializer = new DefaultTextInitializer<String>();
				initializer.setCoTextComponent(view.getAuthorTextField());
				initializer.setCoFailureComponent(view.getFailureTextField());
				authorCobean = initializer.initialize();
			}

			// Bind the component beans to the model.
			{
				BoundPropBinding binding = new BoundPropBinding();
				binding.setModelPath("books");
				binding.setCompProperty("value");
				model = binding.bind(model, tableCobean);
			}
			{
				BoundPropBinding binding = new BoundPropBinding();
				binding.setModelPath("selectedBook");
				binding.setCompProperty("selectedValue");
				model = binding.bind(model, tableCobean);
			}
			{
				BoundPropBinding binding = new BoundPropBinding();
				binding.setModelPath("selectedBook.title");
				binding.setCompProperty("value");
				model = binding.bind(model, titleCobean);
			}
			{
				BoundPropBinding binding = new BoundPropBinding();
				binding.setModelPath("selectedBook.year");
				binding.setCompProperty("value");
				model = binding.bind(model, yearCobean);
			}
			{
				BoundPropBinding binding = new BoundPropBinding();
				binding.setModelPath("selectedBook.author.name");
				binding.setCompProperty("value");
				model = binding.bind(model, authorCobean);
			}

		}
	}

	public SingleSelectionModel getModel() {
		return model;
	}

	public void setModel(SingleSelectionModel model) {
		this.model = model;
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
